<?php

namespace app\common\model;

use think\Exception;

class PyRecommend extends BaseModel
{
    //添加数据
    public function add($data){
        $data=$this->dataFilter($data);
        $rec_title=$data['rec_title'];
        $isset=$this->where("rec_title","=",$rec_title)
            ->where("rec_role","=",$data['rec_role'])
            ->find();
        if($isset){
            return "标题已存在";
        }
        try{
            $this->insert($data);
        }catch (Exception $exception){
            return "数据插入异常";
        }
        return 1;
    }


    //数据编辑
    public function edit($data,$rec_id){
        //防止没有传入序号
        if($data['rec_order']==""){
            $data['rec_order']=2;
        }
        if($data['rec_image']==""){
            return "请上传图片";
        }
        //判断是否标题重复
        $isset=$this->where("rec_title","=",$data['rec_title'])
            ->where("rec_id","<>",$rec_id)
            ->where("rec_role","=",$data['rec_role'])
            ->find();
        if($isset){
            return "标题重复";
        }

        $data=$this->dataFilter($data);

        try{
            $result=$this->where("rec_id","=",$rec_id)->update($data);
        }catch (Exception $exception){
            return "数据编辑失败";
        }

        if(!$result){
            return "数据未发生变化";
        }else{
            return 1;
        }

    }

    //给读取的rec_url加上默认值
    public function getRecUrlAttr($value){
        return $value!=""?$value:"#";
    }
}
